package org.kamimark.copyright;

/** Copyright (c) 2008-2009, Markos Evlogimenos
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * The name of Markos Evlogimenos may be used to endorse or
 *		 promote products derived from this software without specific
 *		 prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CopyrightInserter
{

	public File root;
	private String notice = "/** Copyright (c) 2008-2009, Markos Evlogimenos\n" + " *\n" + " * All rights reserved.\n" + " *\n"
	        + " * Redistribution and use in source and binary forms, with or without\n" + " * modification, are permitted provided that the following conditions\n" + " * are met:\n" + " *\n"
	        + " *     * Redistributions of source code must retain the above copyright\n" + " *       notice, this list of conditions and the following disclaimer.\n"
	        + " *     * Redistributions in binary form must reproduce the above copyright\n" + " *       notice, this list of conditions and the following disclaimer in\n"
	        + " *       the documentation and/or other materials provided with the\n" + " *       distribution.\n" + " *     * The name of Markos Evlogimenos may be used to endorse or\n"
	        + " *		 promote products derived from this software without specific\n" + " *		 prior written permission.\n" + " *\n"
	        + " * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n" + " * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
	        + " * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n" + " * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n"
	        + " * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n" + " * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
	        + " * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n" + " * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
	        + " * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n" + " * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
	        + " * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */\n\n";

	public static void main(String[] args) throws IOException {
		CopyrightInserter p = new CopyrightInserter();
		p.root = new File(args[0]);
		p.process(p.root);
	}

	private void process(File file) throws IOException {
		boolean isDir = file.isDirectory();
		root = isDir ? file : file.getParentFile();
		if (isDir)
			for (File afile : file.listFiles(getJavaFilesOnly()))
				process(afile);
		else
			parse(file);
	}

	private FileFilter getJavaFilesOnly() {
		return new FileFilter()
		{
			public boolean accept(File file) {
				return (file.getName().endsWith("java") || file.isDirectory()) && file.getName().charAt(0) != '.';
			}
		};
	}

	private void parse(File file) throws IOException {
		FileReader fr;
		fr = new FileReader(file);
		BufferedReader br = new BufferedReader(fr);
		String s;
		if ((s = br.readLine()) != null) {
			if (s.charAt(0) == '/' && s.charAt(1) == '*')
				findEnd(file, br);
			else {
				br.close();
				fr = new FileReader(file);
				br = new BufferedReader(fr);
			}
		}
		buildNewFile(file, br);
		br.close();
	}

	private void findEnd(File file, BufferedReader br) throws IOException {
		String s;
		while ((s = br.readLine()) != null) {
			s = s.trim();
			int len = s.length();
			if (len > 1)
				if (s.charAt(len - 2) == '*' && s.charAt(len - 1) == '/')
					break;
		}
	}

	private void buildNewFile(File file, BufferedReader br) throws IOException {
		StringBuilder sb = new StringBuilder(notice);
		String s;
		while ((s = br.readLine()) != null) {
			sb.append(s);
			sb.append('\n');
		}
		saveFile(file, sb);
	}

	private void saveFile(File file, StringBuilder sb) throws IOException {
		file.delete();
		file.createNewFile();
		FileWriter fw = new FileWriter(file);
		fw.append(sb.toString());
		fw.close();
	}

}
